/*
 * Created by JFormDesigner on Thu Jan 05 12:04:56 CST 2023
 */

package otherSAMS.ui;

import otherSAMS.dao.UserDao;
import otherSAMS.model.User;
import otherSAMS.util.ListToArray;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * @author txx
 */
public class UserManage extends JFrame {
    public UserManage() {
        initComponents();
    }
    public static void main(String[] args){
        UserManage userManage  = new UserManage();
        userManage.setVisible(true);
    }

    private void selectMouseClicked(MouseEvent e) {
        String username = userNameField.getText().trim();
        UserDao userDao = new UserDao();
       List<User>userList = userDao.selectAll(1,1000);
       String[][] data =null;
       if(userList != null&& userList.size()>0){
           data = ListToArray.listToArrayWay(userList);
       }else{
           JOptionPane.showMessageDialog(this,"无结果");
           data =new String[0][];
       }
        table1.setModel(new DefaultTableModel(
                data,tableColumeTitle
        ));
        scrollPane1.setViewportView(table1);
        // TODO add your code here
    }

    private void addMouseClicked(MouseEvent e) {
        // TODO add your code here
        AddUser add  = new AddUser();
        add.setVisible(true);
    }

    private void retur(ActionEvent e) {
        // TODO add your code here
        this.dispose();
        UserLogin userLogin = new UserLogin();
        userLogin.initComponents();
        userLogin.setVisible(true);
    }

    private void exit(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }

    private void deleteMouseClicked(MouseEvent e) {
        // TODO add your code here
        Delete delete  = new Delete();
        delete.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        menuBar1 = new JMenuBar();
        menu1 = new JMenu();
        retur = new JButton();
        exit = new JButton();
        panel1 = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        userNameField = new JTextField();
        select = new JButton();
        add = new JButton();
        update = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        delete = new JButton();

        //======== this ========
        setTitle("\u6b22\u8fce\u4f7f\u7528\u5b66\u751f\u6210\u7ee9\u7ba1\u7406\u7cfb\u7edf\uff01");
        setPreferredSize(new Dimension(600, 400));
        Container contentPane = getContentPane();

        //======== menuBar1 ========
        {
            menuBar1.setBackground(new Color(0x99ffff));

            //======== menu1 ========
            {
                menu1.setText("\u7528\u6237\u83dc\u5355");
                menu1.setBackground(new Color(0x33ccff));

                //---- retur ----
                retur.setText("\u8fd4\u56de");
                retur.addActionListener(e -> retur(e));
                menu1.add(retur);

                //---- exit ----
                exit.setText("\u9000\u51fa\u7cfb\u7edf");
                exit.addActionListener(e -> exit(e));
                menu1.add(exit);
            }
            menuBar1.add(menu1);
        }
        setJMenuBar(menuBar1);

        //======== panel1 ========
        {
            panel1.setBackground(new Color(0xccffff));

            //---- label1 ----
            label1.setText("\u7528\u6237\u4fe1\u606f\u7ef4\u62a4");
            label1.setForeground(Color.blue);
            label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));

            //---- label2 ----
            label2.setText("\u7528\u6237\u540d");

            //---- select ----
            select.setText("\u67e5\u8be2");
            select.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    selectMouseClicked(e);
                }
            });

            //---- add ----
            add.setText("\u6dfb\u52a0");
            add.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    addMouseClicked(e);
                }
            });

            //---- update ----
            update.setText("\u4fee\u6539");

            //======== scrollPane1 ========
            {

                //---- table1 ----
                table1.setModel(new DefaultTableModel(
                    new Object[][] {
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                    },
                    new String[] {
                        "\u5e8f\u53f7", "\u7528\u6237\u540d", "\u7528\u6237\u5bc6\u7801", "\u7c7b\u578b", "\u72b6\u6001"
                    }
                ));
                table1.setBackground(Color.cyan);
                scrollPane1.setViewportView(table1);
            }

            //---- delete ----
            delete.setText("\u5220\u9664");
            delete.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    deleteMouseClicked(e);
                }
            });

            GroupLayout panel1Layout = new GroupLayout(panel1);
            panel1.setLayout(panel1Layout);
            panel1Layout.setHorizontalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(label2)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(userNameField, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(select)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(add)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(update)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(delete))
                            .addComponent(label1))
                        .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(scrollPane1)
            );
            panel1Layout.setVerticalGroup(
                panel1Layout.createParallelGroup()
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addComponent(label1)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label2)
                                .addComponent(userNameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(select)
                                .addComponent(add)
                                .addComponent(update)
                                .addComponent(delete)))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE)
                        .addGap(92, 92, 92))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addComponent(panel1, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JMenuBar menuBar1;
    private JMenu menu1;
    private JButton retur;
    private JButton exit;
    private JPanel panel1;
    private JLabel label1;
    private JLabel label2;
    private JTextField userNameField;
    private JButton select;
    private JButton add;
    private JButton update;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton delete;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    private String[] tableColumeTitle = { "\u5e8f\u53f7", "\u7528\u6237\u540d", "\u7528\u6237\u5bc6\u7801", "\u7c7b\u578b", "\u72b6\u6001"};
}
